package com.example.myapplication


import com.example.myapplication.base.ClockActionSheetEvent
import com.example.myapplication.base.LanguageActionSheetEvent

import com.example.myapplication.base.NavBar
import com.example.sihookuiklydemo.base.BasePager
import com.example.sihookuiklydemo.base.BridgeModule
import com.example.sihookuiklydemo.base.MsgDialog
import com.example.sihookuiklydemo.base.MySwitch
import com.example.sihookuiklydemo.base.NotifyName
import com.example.sihookuiklydemo.base.NotifyName.Companion.DESK_MODE_LIST
import com.example.sihookuiklydemo.base.NotifyName.Companion.DEVICE_CONNECT_STATE
import com.example.sihookuiklydemo.base.NotifyName.Companion.DEVICE_HEIGHT
import com.example.sihookuiklydemo.base.NotifyName.Companion.DEVICE_NAME
import com.example.sihookuiklydemo.base.NotifyName.Companion.DEVICE_UNBIND
import com.example.sihookuiklydemo.base.ToastDialog
import com.example.sihookuiklydemo.base.ToastGravity
import com.example.sihookuiklydemo.base.setTimeout
import com.example.sihookuiklydemo.bean.CustomDeskModeJson
import com.example.sihookuiklydemo.bean.getDeskModeList
import com.example.sihookuiklydemo.ble.ADDRESS_DESK_LANGUAGE
import com.example.sihookuiklydemo.ble.ADDRESS_DESK_LANGUAGE_GET
import com.example.sihookuiklydemo.ble.ADDRESS_DESK_lOCK
import com.example.sihookuiklydemo.ble.ADDRESS_DESK_lOCK_GET
import com.example.sihookuiklydemo.ble.ADDRESS_DESK_HEIGHT_GET
import com.example.sihookuiklydemo.ble.ADDRESS_DESK_MODE_GET
import com.example.sihookuiklydemo.ble.ADDRESS_DESK_TIMER_GET
import com.example.sihookuiklydemo.ble.ADDRESS_DESK_TIMER
import com.example.sihookuiklydemo.ble.byteToIntValue
import com.example.sihookuiklydemo.ble.changeLanguage
import com.example.sihookuiklydemo.ble.changeModeHeight0
import com.example.sihookuiklydemo.ble.changeModeHeight1
import com.example.sihookuiklydemo.ble.changeModeHeight2
import com.example.sihookuiklydemo.ble.enableLock
import com.example.sihookuiklydemo.ble.getDataType
import com.example.sihookuiklydemo.ble.getHexData
import com.example.sihookuiklydemo.ble.getNewFloatPx
import com.example.sihookuiklydemo.ble.getNewPx
import com.example.sihookuiklydemo.ble.getValue
import com.example.sihookuiklydemo.ble.readData
import com.example.sihookuiklydemo.ble.sendNotifyList
import com.example.sihookuiklydemo.ble.setHeightMode
import com.example.sihookuiklydemo.net.Api.Companion.baseUrl
import com.example.sihookuiklydemo.theme.ThemeManager

import com.example.sihookuiklydemo.utils.getCancelText
import com.example.sihookuiklydemo.utils.getConfirmText
import com.example.sihookuiklydemo.utils.getLanguage
import com.example.sihookuiklydemo.utils.getShowHeightString

import com.example.sihookuiklydemo.utils.toFeetInches


import com.tencent.kuikly.core.annotations.Page
import com.tencent.kuikly.core.base.Attr
import com.tencent.kuikly.core.base.Border
import com.tencent.kuikly.core.base.BorderStyle
import com.tencent.kuikly.core.base.Color
import com.tencent.kuikly.core.base.ViewBuilder
import com.tencent.kuikly.core.base.ViewRef
import com.tencent.kuikly.core.base.attr.ImageUri
import com.tencent.kuikly.core.coroutines.GlobalScope
import com.tencent.kuikly.core.coroutines.async
import com.tencent.kuikly.core.coroutines.delay
import com.tencent.kuikly.core.coroutines.launch
import com.tencent.kuikly.core.directives.vif
import com.tencent.kuikly.core.layout.Frame
import com.tencent.kuikly.core.log.KLog
import com.tencent.kuikly.core.manager.PagerManager
import com.tencent.kuikly.core.module.CallbackRef
import com.tencent.kuikly.core.module.NetworkModule
import com.tencent.kuikly.core.module.NotifyModule
import com.tencent.kuikly.core.module.RouterModule
import com.tencent.kuikly.core.module.SharedPreferencesModule
import com.tencent.kuikly.core.nvi.serialization.json.JSONArray
import com.tencent.kuikly.core.nvi.serialization.json.JSONObject
import com.tencent.kuikly.core.pager.Pager
import com.tencent.kuikly.core.reactive.handler.observable
import com.tencent.kuikly.core.reactive.handler.observableList
import com.tencent.kuikly.core.views.AlertDialog
import com.tencent.kuikly.core.views.DivView
import com.tencent.kuikly.core.views.Image
import com.tencent.kuikly.core.views.ListView
import com.tencent.kuikly.core.views.Modal
import com.tencent.kuikly.core.views.RichText
import com.tencent.kuikly.core.views.Scroller
import com.tencent.kuikly.core.views.Span
import com.tencent.kuikly.core.views.Text
import com.tencent.kuikly.core.views.TextAttr
import com.tencent.kuikly.core.views.TextView
import com.tencent.kuikly.core.views.View

import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine


@Page("test")
internal class TestPage : BasePager() {
    companion object {

        private const val VIEW_PADDING = 15.0f
        private const val VIEW_SPACING = 2
        private const val Radius = 20f

    }

    lateinit var eventCallbackRef: CallbackRef
    lateinit var devcieUnbindbackRef: CallbackRef
    lateinit var devcieConnectStatebackRef: CallbackRef
    lateinit var deskModeListCallbackRef: CallbackRef
    private var viewWidth: Float by observable(0.0f)
    var heightValue: Int by observable(0)
    var isLock: Boolean by observable(false)
    var showAlarmDialog: Boolean by observable(false)
    var showLanguageDialog: Boolean by observable(false)
    var showDeskModeDialog: Boolean by observable(false)
    var diyModeSelectedIndex: Int by observable(0)
    var modeSelected = false
    var currentMode by observable(0)
    var deskModeList by observableList<CustomDeskModeJson>()
    var seletedDeskMode: CustomDeskModeJson? by observable(null)
    var showAlert by observable(false)
    var currentLanuage by observable(0)
    var isClockEnableText by observable("未开启")
    var clockMin by observable(0)
    var deskTop by observable(25f)

    var deskBmHeight = 275f
    var deskBmWidth = 220f
    var deskTopHeight = 286f
    var buttonWidth = 32f
    var buttonAllWidth = 36f
    var buttonAllHeight = 100f
    var showMsgDialog by observable(false)
    var toastMsg by observable("")
    var titleDeviceName by observable("")
    lateinit var ref: ViewRef<DivView>
    override fun body(): ViewBuilder {
        val ctx = this
        return {
            attr {
                flexDirectionColumn()
            }
            Image {
                attr {
                    absolutePosition(0f, 0f, 0f, 0f)
                    src(
                        ThemeManager.getAssetUri(
                            if (ctx.isNightMode()) "dark" else "light",
                            "device_home_bg.png"
                        )
                    )


                }
            }
            NavBar {
                attr {
                    title = ctx.titleDeviceName
                   // title="ssssssssssssssssssssssssssssssssssssssssssssssssss"
                    isDarkMode = ctx.isNightMode()
                }
                event {
                    onClick {
                        /* val bridgeModule =
                             PagerManager.getCurrentPager().getModule<Brid
                             geModule>(BridgeModule.MODULE_NAME)*/
                        var key = "isBind"
                        var isBind = ctx.pagerData.params.optBoolean("isBind")
                        ctx.acquireModule<BridgeModule>(BridgeModule.MODULE_NAME)
                            .closePage(key, isBind)
                        // ctx.acquireModule<RouterModule>(RouterModule.MODULE_NAME).closePage()
                        KLog.i("333333", "点击")
                    }
                    onRightClick {
                        ctx.openNameWithTitle(
                            "Setting",
                            ctx.getLanguage("device_more_setting", "更多设置")
                        )
                        /*ctx.acquireModule<RouterModule>(RouterModule.MODULE_NAME)
                            .openPage("Setting", ctx.pagerData.params)*/
                    }
                }
            }

            Scroller {
                attr {
                    flex(1f)
                    marginBottom(136f)
                }
                apply(ctx.connectStateView())
                apply(ctx.getDeskTop())
                View {
                    ref {
                        ctx.ref = it
                    }
                    attr {
                        flexDirectionRow()
                        justifyContentFlexStart()
                        alignItemsStretch()
                        width(ctx.pagerData.pageViewWidth)
                        //  size(ctx.pagerData.pageViewWidth, getNewFloatPx(80f))
                        margin(left = VIEW_PADDING, right = VIEW_PADDING)
                    }
                    apply(ctx.modeView())
                    apply(ctx.clockView())

                }

                apply(ctx.languageView())
                apply(ctx.lockView())

                vif({ ctx.isLock }) {
                    MsgDialog {
                        attr {
                            showAlert(ctx.showAlert)
                            title(
                                getLanguage(
                                    "desk_lock_prompt",
                                    "智能电动升降桌童锁已锁定，当前不可控制，是否解锁?"
                                )
                            )
                            inWindow(true)

                            actionButtons(getCancelText(), getConfirmText())
                        }
                        event {
                            clickActionButton { index ->
                                // 根据index进行确认点击了哪一个button处理对应事件(index值和actionButtons传入button的下标一致)
                                ctx.showAlert = false  // 关闭Alert弹框
                                if (index == 1) {
                                    var sendList = JSONArray()
                                    sendList.put(enableLock(false).toList())
                                    sendList.put(readData(ADDRESS_DESK_lOCK_GET).toList())
                                    sendNotifyList(ctx, NotifyName.SEND_VALUE_ARRAY, sendList)
                                }
                            }
                            willDismiss {
                                // 按下系统返回按键或右滑返回时触发
                                ctx.showAlert = false  // 关闭Alert弹框
                            }
                        }
                    }
                }

            }
            apply(ctx.selectedModeView())
            apply(ctx.getMsgView())
        }


    }

    var modePicSize = 50f
    var modeViewSize = 70f

    fun selectedModeView(): ViewBuilder {
        var ctx = this@TestPage
        return {
            View {
                attr {
                    absolutePosition(bottom = 32f)

                    justifyContentSpaceEvenly()
                    margin(left = VIEW_PADDING, right = VIEW_PADDING)
                    size(
                        ctx.pagerData.pageViewWidth - VIEW_PADDING * 2,
                        91f.getNewPx(this.pagerData)
                    )
                    backgroundColor(ctx.theme.colors.bg_2)
                    flexDirectionRow()
                    borderRadius(50f)

                }
                View {
                    attr {
                        if (ctx.diyModeSelectedIndex == 1) {
                            border(
                                Border(
                                    2f,
                                    BorderStyle.SOLID,
                                    Color(Color.parseString16ToLong("ffF6C57F"))
                                )
                            )
                        } else {
                            border(Border(2f, BorderStyle.SOLID, Color.TRANSPARENT))
                        }
                        borderRadius(Radius)
                        size(getNewFloatPx(ctx.modeViewSize), getNewFloatPx(ctx.modeViewSize))

                        alignSelfCenter()
                        allCenter()
                    }
                    Image {
                        attr {
                            resizeStretch()
                            size(getNewFloatPx(ctx.modePicSize), getNewFloatPx(ctx.modePicSize))

                            src(ImageUri.commonAssets("sittingposture_1.png"))

                        }
                    }
                    event {
                        click {
                            ctx.changeHeight(1)

                        }
                    }
                }
                View {

                    attr {
                        if (ctx.diyModeSelectedIndex == 2) {
                            border(
                                Border(
                                    2f,
                                    BorderStyle.SOLID,
                                    Color(Color.parseString16ToLong("ff74B0F1"))
                                )
                            )
                        } else {
                            border(Border(2f, BorderStyle.SOLID, Color.TRANSPARENT))
                        }
                        borderRadius(Radius)
                        size(getNewFloatPx(ctx.modeViewSize), getNewFloatPx(ctx.modeViewSize))
                        alignSelfCenter()
                        allCenter()
                    }
                    Image {
                        attr {
                            resizeCover()
                            size(getNewFloatPx(ctx.modePicSize), getNewFloatPx(ctx.modePicSize))
                            alignSelfCenter()
                            src(ImageUri.commonAssets("sittingposture_2.png"))
                        }
                    }
                    event {
                        click {

                            ctx.changeHeight(2)
                        }
                    }
                }
                View {
                    attr {
                        if (ctx.diyModeSelectedIndex == 3) {
                            border(
                                Border(
                                    2f,
                                    BorderStyle.SOLID,
                                    Color(Color.parseString16ToLong("ff7F9DEF"))
                                )
                            )
                        } else {
                            border(Border(2f, BorderStyle.SOLID, Color.TRANSPARENT))
                        }
                        borderRadius(Radius)
                        size(getNewFloatPx(ctx.modeViewSize), getNewFloatPx(ctx.modeViewSize))
                        alignSelfCenter()
                        allCenter()
                    }
                    event {
                        click {
                            ctx.changeHeight(3)
                        }
                    }
                    Image {
                        attr {
                            resizeContain()
                            size(getNewFloatPx(ctx.modePicSize), getNewFloatPx(ctx.modePicSize))
                            alignSelfCenter()

                            src(ImageUri.commonAssets("sittingposture_3.png"))
                        }
                    }
                }
                apply(ctx.addMaskView())
            }
        }
    }

    var connectState by observable(DEVICESTATE.DISCONNECTED)

    enum class DEVICESTATE {
        CONNECTED, DISCONNECTED, CONNECTING
    }

    fun connectStateView(): ViewBuilder {
        var ctx = this@TestPage
        return {
            View {
                attr {
                    allCenter()
                    flexDirectionRow()
                    absolutePosition(top = 0f, left = 0f, right = 0f)
                }
                Image {
                    attr {
                        var src = when (ctx.connectState) {
                            DEVICESTATE.CONNECTED -> {
                                ImageUri.commonAssets("tp_bluetooth_hover.png")
                            }

                            DEVICESTATE.DISCONNECTED -> {
                                ImageUri.commonAssets("tp_bluetooth_disconnect.png")
                            }

                            else -> {
                                ImageUri.commonAssets("tp_bluetooth_disconnect.png")
                            }
                        }
                        src(src)
                        size(16f, 16f)
                    }
                }
                Text {
                    attr {
                        var showText = ""
                        var showColor = Color("ff3599FF")
                        when (ctx.connectState) {
                            DEVICESTATE.CONNECTED -> {
                                showText = getLanguage("device_connected", "已连接")
                                showColor = Color(0xff3599FF)
                            }

                            DEVICESTATE.DISCONNECTED -> {
                                showText = getLanguage("device_disconnected", "未连接")
                                showColor = Color(0xff6A7178)
                            }

                            else -> {
                                showText = getLanguage("device_connecting", "正在连接...")
                                showColor = Color(0xff6A7178)
                            }
                        }
                        text(showText)
                        useDpFontSizeDim(true)
                        fontSize(12f)
                        color(showColor)
                    }
                }
            }
        }
    }


    fun modeView(): ViewBuilder {
        var ctx = this@TestPage
        return {

            View {
                ref { ctx.ref = it }
                attr {

                    width(ctx.viewWidth)
                    setTimeout(50) {
                        ctx.ref.view?.parent?.flexNode?.layoutHeight?.let {
                            height(it)
                        }

                        //KLog.i("clockView","${ ctx.ref.view?.parent?.flexNode?.layoutHeight}")

                    }
                    // size(ctx.viewWidth, 80f.getNewPx(this.pagerData))
                    marginRight(getNewFloatPx(VIEW_SPACING.toFloat()))
                    backgroundColor(ctx.theme.colors.bg_2)
                    flexDirectionRow()
                    borderRadius(Radius)
                }
                event {
                    click {
                        ctx.showDeskModeDialog = true
                    }
                }
                Image {
                    attr {
                        alignSelfCenter()
                        marginLeft(getNewFloatPx(24f))
                        size(getNewFloatPx(32f), getNewFloatPx(32f))
                        resizeContain()
                        src(ImageUri.commonAssets("desk_device_home_mode.png"))
                    }
                }
                View {
                    attr {

                        marginLeft(getNewFloatPx(2f))
                        flexDirectionColumn()
                    }
                    Text {
                        attr {
                            marginTop(getNewFloatPx(22f))
                            maxWidth(ctx.viewWidth - 60f)
                            fontSize(getNewFloatPx(15f))

                            text(ctx.getLanguage("desk_change_mode", "切换模式"))

                            color(ctx.theme.colors.mainTextColor)
                            useDpFontSizeDim(true)
                        }
                    }
                    Text {
                        attr {
                            var modeName = "智能模式"
                            color(ctx.theme.colors.mainTextColor)
                            useDpFontSizeDim(true)
                            maxWidth(ctx.viewWidth - 60f)
                            marginBottom(22f)
                            ctx.seletedDeskMode?.name?.let {

                                modeName = it

                            }
                            var showName = if (modeName == "智能模式") getLanguage(
                                "desk_auto_mode",
                                "智能模式"
                            ) else modeName
                            text(showName)
                            marginTop(4f)
                            fontSize(getNewFloatPx(12f))
                            opacity(0.4f)
                        }
                    }
                    vif({ ctx.showDeskModeDialog }) {
                        Modal {
                            DeskModeActionSheetViewEvent {
                                attr {
                                    currentMode = ctx.currentMode

                                }
                                event {
                                    onCancelClick {
                                        ctx.showDeskModeDialog = false
                                    }
                                    onConfirmClick { customDeskModeJson, position ->
                                        ctx.currentMode = position
                                        ctx.showDeskModeDialog = false
                                        ctx.seletedDeskMode = customDeskModeJson

                                        ctx.updateDeskMode(position)


                                    }

                                }
                            }
                        }
                    }

                }
                apply(ctx.addMaskView())

            }
        }
    }

    fun clockView(): ViewBuilder {
        var ctx = this@TestPage
        return {
            View {
                attr {
                    width(ctx.viewWidth)
                    marginLeft(getNewFloatPx(VIEW_SPACING.toFloat()))
                    backgroundColor(ctx.theme.colors.bg_2)
                    borderRadius(Radius)
                    flexDirectionRow()
                    //
                    setTimeout(50) {
                        ctx.ref.view?.parent?.flexNode?.layoutHeight?.let {
                            height(it)
                        }

                        //KLog.i("clockView","${ ctx.ref.view?.parent?.flexNode?.layoutHeight}")

                    }
                }
                event {
                    click {
                        ctx.showAlarmDialog = true
                    }
                }
                Image {
                    attr {
                        alignSelfCenter()
                        marginLeft(getNewFloatPx(24f))
                        size(getNewFloatPx(32f), getNewFloatPx(32f))
                        resizeContain()
                        src(ImageUri.commonAssets("desk_device_home_sound.png"))
                    }
                }
                View {
                    attr {
                        marginLeft(getNewFloatPx(2f))
                        flexDirectionColumn()
                    }
                    Text {
                        attr {
                            marginTop(getNewFloatPx(22f))
                            fontSize(getNewFloatPx(15f))
                            maxWidth(ctx.viewWidth - 60f)
                            text(ctx.getLanguage("desk_alarm_reminder", "闹钟提醒"))

                            color(ctx.theme.colors.mainTextColor)
                            useDpFontSizeDim(true)
                        }
                    }
                    Text {
                        attr {
                            maxWidth(ctx.viewWidth - 60f)
                            marginBottom(22f)
                            text(ctx.isClockEnableText)
                            marginTop(4f)
                            fontSize(getNewFloatPx(12f))
                            opacity(0.4f)
                            color(ctx.theme.colors.mainTextColor)
                            useDpFontSizeDim(true)
                        }
                    }

                    vif({ ctx.showAlarmDialog }) {
                        Modal {
                            ClockActionSheetEvent {
                                attr {
                                    currentClockValue = ctx.clockMin
                                }
                                event {
                                    close {
                                        ctx.showAlarmDialog = false
                                        KLog.i("close", it.toString())
                                    }
                                }
                            }
                        }
                    }

                }
                apply(ctx.addMaskView())
            }
        }
    }

    fun lockView(): ViewBuilder {

        var ctx = this@TestPage
        return {
            View {
                attr {
                    marginTop(getNewFloatPx(12f))

                    margin(left = VIEW_PADDING, right = VIEW_PADDING)
                    /* size(
                         ctx.pagerData.pageViewWidth - VIEW_PADDING * 2,
                         80f.getNewPx(this.pagerData)
                     )*/
                    minHeight(80f)
                    width(ctx.pagerData.pageViewWidth - VIEW_PADDING * 2)
                    backgroundColor(ctx.theme.colors.bg_2)
                    flexDirectionRow()
                    borderRadius(Radius)
                }
                Image {
                    attr {
                        size(getNewFloatPx(36f), getNewFloatPx(36f))
                        alignSelfCenter()
                        marginLeft(getNewFloatPx(24f))
                        src(ImageUri.commonAssets("desk_device_home_lock.png"))
                    }
                }
                View {
                    attr {
                        width(ctx.pagerData.pageViewWidth - VIEW_PADDING * 4 - 80f - 36f - 16f)
                        marginLeft(getNewFloatPx(2f))
                        flexDirectionColumn()
                    }
                    Text {
                        attr {
                            marginTop(getNewFloatPx(22f))
                            fontSize(getNewFloatPx(15f))

                            text(ctx.getLanguage("desk_lock", "童锁"))

                            useDpFontSizeDim(true)
                            color(ctx.theme.colors.mainTextColor)
                        }

                    }
                    Text {
                        attr {

                            text(ctx.getLanguage("desk_lock_sub", "锁定后智能电动升降桌不可升降"))

                            useDpFontSizeDim(true)
                            color(ctx.theme.colors.mainTextColor)
                            marginTop(4f)
                            marginBottom(16f)
                            fontSize(getNewFloatPx(12f))
                            opacity(0.4f)
                        }
                    }

                }
                View {
                    attr { flex(1f) }
                }
                MySwitch {
                    attr {

                        isOn(ctx.isLock)
                        alignSelfCenter()
                        marginRight(16f)
                    }
                    event {
                        switchOnChanged {
                            KLog.i("switch", "$it")

                            if (it) {
                                ctx.modeSelected = false
                                ctx.diyModeSelectedIndex = 0
                            }
                            var sendList = JSONArray()
                            sendList.put(enableLock(it).toList())
                            sendList.put(readData(ADDRESS_DESK_lOCK_GET).toList())
                            sendNotifyList(ctx, NotifyName.SEND_VALUE_ARRAY, sendList)
                            //  ctx.sendNotify(NotifyName.SEND_VALUE, sendValue)

                        }
                    }
                }
                apply(ctx.addMaskView())
            }
        }
    }

    fun languageView(): ViewBuilder {
        var ctx = this@TestPage
        return {
            View {

                attr {
                    marginTop(getNewFloatPx(12f))

                    margin(left = VIEW_PADDING, right = VIEW_PADDING)
                    KLog.i("TAG", "languageView: ${80f.getNewPx(this.pagerData)}")
                    /* size(
                         ctx.pagerData.pageViewWidth - VIEW_PADDING * 2,
                         80f.getNewPx(this.pagerData)
                     )*/
                    width(ctx.pagerData.pageViewWidth - VIEW_PADDING * 2)

                    backgroundColor(ctx.theme.colors.bg_2)
                    flexDirectionRow()
                    borderRadius(Radius)
                }
                event {
                    click {
                        ctx.showLanguageDialog = true
                    }
                }
                Image {
                    attr {
                        size(getNewFloatPx(36f), getNewFloatPx(36f))
                        alignSelfCenter()

                        marginLeft(getNewFloatPx(24f))
                        src(ImageUri.commonAssets("desk_device_home_lanuage.png"))
                    }
                }
                View {
                    attr {
                        marginLeft(getNewFloatPx(2f))
                        flexDirectionColumn()
                    }

                    Text {
                        attr {
                            marginTop(this@TestPage.getNewFloatPx(22f))
                            fontSize(this@TestPage.getNewFloatPx(15f))

                            text(ctx.getLanguage("desk_change_lanuage", "语言切换"))
                            color(ctx.theme.colors.mainTextColor)
                            useDpFontSizeDim(true)
                        }

                    }
                    Text {
                        attr {
                            color(ctx.theme.colors.mainTextColor)
                            maxWidth(ctx.pagerData.pageViewWidth - VIEW_PADDING * 2 - 64f)
                            text(ctx.getLanguage("desk_change_lanuage_sub", "快速切换电动桌语言"))
                            marginTop(4f)
                            marginBottom(22f)
                            fontSize(this@TestPage.getNewFloatPx(12f))
                            opacity(0.4f)
                            useDpFontSizeDim(true)
                        }
                    }
                    vif({ ctx.showLanguageDialog }) {
                        Modal {
                            LanguageActionSheetEvent {
                                attr { currentIndex = ctx.currentLanuage }
                                event {
                                    onCancelClick { ctx.showLanguageDialog = false }
                                    onConfirmClick { deviceName, position ->
                                        ctx.showLanguageDialog = false
                                        KLog.i("close", deviceName)
                                        ctx.currentLanuage = position
                                        var sendValue = changeLanguage(position)
                                        ctx.sendNotify(NotifyName.SEND_VALUE, sendValue)
                                    }

                                }
                            }
                        }
                    }
                }
                apply(ctx.addMaskView())
            }
        }
    }

    fun addMaskView(): ViewBuilder {
        var ctx = this@TestPage
        return {
            vif({ ctx.connectState != DEVICESTATE.CONNECTED }) {
                View {
                    attr {
                        absolutePosition(0f, 0f, 0f, 0f)
                        backgroundColor(ctx.theme.colors.bg_2a)


                    }
                    event {
                        click {
                            KLog.i("ss", "mask")
                        }
                    }
                }
            }
        }
    }

    fun getMsgView(): ViewBuilder {
        var ctx = this@TestPage
        return {
            vif({ ctx.showMsgDialog }) {
                Modal {
                    ToastDialog {
                        attr {
                            msg = ctx.toastMsg
                            toastGravity = ToastGravity.TOP
                        }
                        event {
                            onDissMiss {
                                ctx.showMsgDialog = false
                            }
                        }
                    }
                }
            }
        }
    }

    fun getCurrentHeightView(): ViewBuilder {
        var heightUnit = pagerData.params.optInt(NotifyName.HEIGHT_UNIT)
        var ctx = this
        return {
            vif({ heightUnit == 0 }) {
                RichText {
                    Span {
                        fontSize(28f)
                        text("${ctx.heightValue}")
                    }
                    Span {
                        fontSize(15f)
                        text("cm")
                    }
                    attr {
                        maxWidth(120f)
                        useDpFontSizeDim(true)
                        color(ctx.theme.colors.mainTextColor)

                    }

                }
            }
            vif({ heightUnit == 1 }) {

                RichText {
                    val (feet, inches) = ctx.heightValue.toFeetInches()
                    Span {

                        fontSize(28f)
                        text("${feet}")
                    }
                    Span {
                        val ftString = getLanguage("common_unit_foot", "")
                        fontSize(15f)
                        text("${ftString}")
                    }
                    Span {
                        fontSize(28f)
                        text("${inches}")
                    }
                    Span {

                        val inString = getLanguage("common_unit_inch", "")
                        fontSize(15f)
                        text("${inString}")
                    }
                    attr {
                        maxWidth(120f)
                        useDpFontSizeDim(true)
                        color(ctx.theme.colors.mainTextColor)

                    }
                }

            }
        }
    }

    fun getDeskTop(): ViewBuilder {
        var ctx = this@TestPage
        return {
            View {
                attr {
                    flexDirectionRow()
                    height(ctx.deskTopHeight)
                }
                Image {
                    attr {
                        margin(top = ctx.deskTop, left = getNewFloatPx(20f))
                        size(ctx.deskBmWidth, ctx.deskBmHeight)
                        resizeCover()
                        src(ImageUri.commonAssets("bg_desk_top.png"))
                    }


                }
                Image {
                    attr {
                        positionAbsolute()
                        top(20f)
                        bottom(0f)
                        size(ctx.deskBmWidth, ctx.deskBmHeight)
                        margin(left = getNewFloatPx(20f))
                        resizeCover()
                        src(ImageUri.commonAssets("bg_desk_bottom.png"))
                    }

                }
                View {
                    attr {
                        flex(1f)
                    }
                }
                View {
                    attr {
                        marginRight(48f)
                        top(63f)
                        flexDirectionColumn()
                        alignItemsFlexEnd()
                    }
                    Text {
                        attr {

                            text(getLanguage("desk_current_height", "当前高度"))
                            useDpFontSizeDim(true)
                            color(Color.parseString16ToLong("ff98A2AC"))
                        }
                    }
                    apply(ctx.getCurrentHeightView())
                    /* Text {
                         attr {
                             maxWidth(110f)
                             useDpFontSizeDim(true)
                             var showText = getShowHeightString(ctx.heightValue, 1)
                             text("${showText}")
                             color(ctx.theme.colors.mainTextColor)
                             fontSize(28f)
                         }

                     }*/
                    View {
                        attr {
                            marginTop(24f)
                            size(ctx.buttonAllWidth, ctx.buttonAllHeight)
                            backgroundColor(Color.WHITE)
                            borderRadius(ctx.buttonAllWidth / 2f)
                            alignItemsCenter()
                        }
                        Image {

                            attr {
                                marginRight(2f)
                                width(ctx.buttonWidth)
                                flex(1f)
                                resizeContain()
                                src(ImageUri.commonAssets("device_home_deup.png"))
                            }
                            event {
                                click {
                                    /*ctx.heightValue++
                                ctx.deskTop--*/
                                    /*ctx.sendNotify(
                                        NotifyName.SEND_VALUE,
                                        changeModeHeight2(ctx.heightValue + 2)
                                    )*/
                                    ctx.changeHeightWithLockCheck(
                                        NotifyName.SEND_VALUE,
                                        changeModeHeight2(ctx.heightValue + 2)
                                    )
                                    //ctx.sendNotify("height", "rise")
                                }
                                longPress { longPressParams ->
                                    val state = longPressParams.state
                                    when (state) {
                                        "start" -> {
                                            KLog.i("长按", "start")
                                            ctx.changeHeightWithLockCheck(
                                                NotifyName.SEND_VALUE, setHeightMode(1)
                                            )
                                            /* ctx.sendNotify(
                                                 NotifyName.SEND_VALUE,
                                                 setHeightMode(1)
                                             )*/
                                        }

                                        "move" -> {
                                            KLog.i("长按", "move")
                                        }

                                        "end" -> {
                                            KLog.i("长按", "end")
                                            /* ctx.sendNotify(
                                                 NotifyName.SEND_VALUE,
                                                 setHeightMode(0)
                                             )*/
                                            ctx.changeHeightWithLockCheck(
                                                NotifyName.SEND_VALUE, setHeightMode(0), 0
                                            )
                                        }
                                    }
                                }
                            }
                        }
                        View {
                            attr {
                                size(ctx.buttonAllWidth, 1f)
                                backgroundColor(Color.GRAY)
                            }
                        }

                        Image {

                            attr {
                                marginRight(2f)
                                width(ctx.buttonWidth)
                                flex(1f)
                                resizeContain()
                                src(ImageUri.commonAssets("device_home_defooter.png"))
                            }
                            event {
                                click {
                                    /*  ctx.heightValue--
                                 ctx.deskTop++*/
                                    KLog.i("点击", "start")
                                    /* ctx.sendNotify(
                                         NotifyName.SEND_VALUE,
                                         changeModeHeight2(ctx.heightValue - 2)
                                     )*/
                                    ctx.changeHeightWithLockCheck(
                                        NotifyName.SEND_VALUE,
                                        changeModeHeight2(ctx.heightValue - 2), 2
                                    )
                                    // ctx.sendNotify("height", "down")
                                }
                                longPress { longPressParams ->
                                    val state = longPressParams.state
                                    when (state) {
                                        "start" -> {
                                            KLog.i("长按", "start")
                                            ctx.changeHeightWithLockCheck(
                                                NotifyName.SEND_VALUE, setHeightMode(2), 2
                                            )

                                        }

                                        "move" -> {
                                            KLog.i("长按", "move")
                                        }

                                        "end" -> {
                                            KLog.i("长按", "end")
                                            ctx.changeHeightWithLockCheck(
                                                NotifyName.SEND_VALUE, setHeightMode(0), 0
                                            )
                                            /* ctx.sendNotify(
                                                 NotifyName.SEND_VALUE,
                                                 setHeightMode(0)
                                             )*/

                                        }
                                    }
                                }
                            }
                        }
                        apply(ctx.addMaskView())
                    }

                }
            }
        }
    }

    /**
     *
     * @param name String
     * @param sendValue ByteArray
     * @param opMode Int 0停止，1升高，2降低
     */
    fun changeHeightWithLockCheck(name: String, sendValue: ByteArray, opMode: Int = 1) {
        if (isLock) {
            showAlert = true
        } else {

            var tipsString = when (heightValue) {
                120 -> {
                    if (opMode == 1) {
                        getLanguage("desk_max_height_reached", "已到达最大高度")
                    } else {
                        null
                    }

                }

                67 -> {
                    if (opMode == 2) {
                        getLanguage("desk_min_height_reached", "已到达最小高度")
                    } else {
                        null
                    }

                }

                else -> {
                    null
                }
            }
            if (tipsString.isNullOrEmpty()) {
                sendNotify(name, sendValue)
            } else {
                toastMsg = tipsString
                showMsgDialog = true

            }

        }
    }

    fun showLockTipsDialog(): ViewBuilder {
        return {
            AlertDialog {
                attr {
                    showAlert(this@TestPage.showAlert)
                    title("智能电动升降桌童锁已锁定，当前不可控制，是否解锁?")
                    inWindow(true)

                }
                event {
                    clickActionButton { index ->
                        // 根据index进行确认点击了哪一个button处理对应事件(index值和actionButtons传入button的下标一致)
                        this@TestPage.showAlert = false  // 关闭Alert弹框
                    }
                    willDismiss {
                        // 按下系统返回按键或右滑返回时触发
                        this@TestPage.showAlert = false  // 关闭Alert弹框
                    }
                }
            }
        }
    }

    fun changeHeight(position: Int) {
        if (isLock) {
            showAlert = true
        } else {
            modeSelected = true
            diyModeSelectedIndex = position
            KLog.i(
                "选择模式",
                "${seletedDeskMode?.sit} ${seletedDeskMode?.stand} ${seletedDeskMode?.custom}"
            )
            var height = when (position) {
                1 -> {
                    seletedDeskMode?.sit?.let {
                        sendNotify(NotifyName.SEND_VALUE, changeModeHeight0(it))
                    }

                }

                2 -> {
                    seletedDeskMode?.stand?.let {
                        sendNotify(NotifyName.SEND_VALUE, changeModeHeight1(it))

                    }

                }

                else -> {
                    seletedDeskMode?.custom?.let {
                        sendNotify(NotifyName.SEND_VALUE, changeModeHeight2(it))
                    }

                }
            }
        }

    }

    fun sendNotify(opName: String, value: ByteArray) {
        acquireModule<NotifyModule>(NotifyModule.MODULE_NAME).postNotify(
            "BLE_OP",
            JSONObject().apply {
                put(opName, value.toList())
            })
    }


    fun getDeskInfo() {

        var jsonArray = JSONArray()
        jsonArray.put(readData(ADDRESS_DESK_LANGUAGE_GET).toList())
        jsonArray.put(readData(ADDRESS_DESK_HEIGHT_GET).toList())
        jsonArray.put(readData(ADDRESS_DESK_lOCK_GET).toList())
        jsonArray.put(readData(ADDRESS_DESK_MODE_GET).toList())
        jsonArray.put(readData(ADDRESS_DESK_TIMER_GET).toList())
        KLog.i("获取设备信息", "$jsonArray")
        sendNotifyList(this, NotifyName.SEND_VALUE_ARRAY, jsonArray)


    }

    override fun created() {
        super.created()
        KLog.i("onCreate", "")
        isClockEnableText = getLanguage("common_closed", "未开启")
        eventCallbackRef =
            acquireModule<NotifyModule>(NotifyModule.MODULE_NAME).addNotify("BleData") { data ->
                var deviceValue = data?.optJSONObject("deviceValue")
                var byteValue = deviceValue?.opt("value")
                KLog.i("TestPage", "$data")
                byteValue?.let {
                    resloveData(getHexData(it as String))
                }
            }
        devcieUnbindbackRef =
            acquireModule<NotifyModule>(NotifyModule.MODULE_NAME).addNotify(DEVICE_UNBIND) { data ->
                acquireModule<RouterModule>(RouterModule.MODULE_NAME).closePage()
            }
        deskModeListCallbackRef =
            acquireModule<NotifyModule>(NotifyModule.MODULE_NAME).addNotify(DESK_MODE_LIST) { data ->
                var content = data?.optJSONObject("content")
                updateDeskListInfo(content)
            }
        devcieConnectStatebackRef =
            acquireModule<NotifyModule>(NotifyModule.MODULE_NAME).addNotify(DEVICE_CONNECT_STATE) { data ->
                var connectStateInt = data?.optInt("connectState")
                KLog.i("设备链接状态", "$connectStateInt")
                connectStateInt?.let {
                    this.connectState = DEVICESTATE.values()[connectStateInt]
                }

            }
        var deskModeListString =
            acquireModule<SharedPreferencesModule>(SharedPreferencesModule.MODULE_NAME)
                .getObject("DeskModeList")
        updateDeskListInfo(deskModeListString)
        var saveHeight = acquireModule<SharedPreferencesModule>(SharedPreferencesModule.MODULE_NAME)
            .getInt(DEVICE_HEIGHT)
        saveHeight?.let {
            heightValue = it
        }
        getDeskInfo()
        queryDeskModeList()

        var width = pageData.pageViewWidth
        var height = pagerData.pageViewHeight
        KLog.i("Width", "$height")
        var isPad = pagerData.params.optBoolean("isPad")
        var deviceInfo = pagerData.params?.optJSONObject("bindDeviceInfo")
        var nativeConnectState = pagerData.params.optInt("connectState")
        nativeConnectState?.let {
            connectState = DEVICESTATE.values()[it]
        }
        deviceInfo?.let {
            it.optString("nameV2")?.let {
                getDeviceShowName(deviceInfo.optString("name"), it)?.let {
                    titleDeviceName = it
                    saveDeviceName(it)
                }

            }
            KLog.i("deviceinfoJson", "$it")
        }
        /*lifecycleScope.launch(Dispatchers.IO) {
            var link=queryLanguageFile()
            var data=downLoadLanguageFile(link)
            acquireModule<SharedPreferencesModule>(SharedPreferencesModule.MODULE_NAME)
                .setObject("LANGUAGE_FILE", data)
        }*/

       /* GlobalScope.launch(Dispatchers.Kuikly[this]) {

            val data = withContext(Dispatchers.IO) { // 在IO线程调用KMP的方法
                var link=queryLanguageFile()
                var data=downLoadLanguageFile(link)
            }

        }*/


        if (isPad) {
            if (pagerData.isIOS) {
                var scale = width / 375f
                deskBmHeight = 256.79f * scale
                deskBmWidth = deskBmHeight - 30f
                deskTopHeight = deskBmHeight + 20f
                buttonWidth = 50f * scale
                buttonAllWidth = 54f * scale
                buttonAllHeight = 140f * scale
            }

        }

    }

    suspend fun queryLanguageFile(): String {
        return suspendCoroutine { con ->
            var pageJson = pageData.params

            pageJson?.let {
                var version = "0.0.0"
                var jsonObject = JSONObject()
                jsonObject.put("configType", "MULTI_LANG")
                jsonObject.put("version", version)
                var jsonObjectHeader = JSONObject()
                jsonObjectHeader.put("Content-Type", "application/json")
                acquireModule<NetworkModule>(NetworkModule.MODULE_NAME).httpRequest(
                    "${baseUrl}app/update/configCheck",
                    true,
                    jsonObject,
                    headers = jsonObjectHeader,
                    responseCallback = { data, success, errorMsg, networkResponse ->
                        var content = data.optJSONObject("content")
                        var code = data.optInt("code")
                        if (networkResponse.statusCode == 200) {
                            if (code == 0) {
                                content?.let {
                                    var isNeedUpdate = content.optBoolean("isNeedUpdate")
                                    if (isNeedUpdate) {
                                        var downLink = content.optString("downLink")
                                        // downLoadLanguageFile(downLink)
                                        con.resume(downLink)
                                    }
                                    KLog.i("testPage", "queryLanguageFile:$it ")
                                }

                            }
                        }


                    })
            }

        }

    }

    suspend fun downLoadLanguageFile(link: String): JSONObject {
        return suspendCoroutine { con ->
            var jsonObject = JSONObject()
            var jsonObjectHeader = JSONObject()
            jsonObjectHeader.put("Content-Type", "application/json")
            jsonObjectHeader.put("Referer", "sihoo-app")
            acquireModule<NetworkModule>(NetworkModule.MODULE_NAME).httpRequest(
                link,
                false,
                jsonObject,
                headers = jsonObjectHeader,
                responseCallback = { data, success, errorMsg, networkResponse ->

                    KLog.i("testPage", "downLoadLanguageFile:$data $networkResponse")

                    if (networkResponse.statusCode == 200) {
                        con.resume(data)
                        /*acquireModule<SharedPreferencesModule>(SharedPreferencesModule.MODULE_NAME)
                            .setObject("LANGUAGE_FILE", data)*/


                    }


                })

        }

    }

    fun getDeviceShowName(name: String, nameV2: String): String? {
        var showName = name
        nameV2?.let {

            if ((it.startsWith("{#") && it.endsWith("}")) || (it.startsWith("{$") && it.endsWith("}"))) {
                var nameKey = getLanguageKey(it)
                showName = getLanguage(nameKey, name)
            } else {
                showName = it
            }
        }
        return showName
    }

    fun getLanguageKey(keyValue: String?): String {
        return keyValue?.subSequence(2, keyValue.length - 1).toString()
    }

    fun resloveData(byteArray: ByteArray?) {
        byteArray?.let {
            var dataType = getDataType(it)
            var cmdData = getValue(it[9], 0)
            var dataValue = byteToIntValue(it, 13)
            when (dataType) {
                ADDRESS_DESK_LANGUAGE, ADDRESS_DESK_LANGUAGE_GET -> {
                    KLog.i("语言", "${dataValue}")
                    currentLanuage = dataValue
                }

                ADDRESS_DESK_lOCK, ADDRESS_DESK_lOCK_GET -> {
                    isLock = dataValue == 1
                    KLog.i("开关", "${dataValue}")
                    //binding.checkboxVChip.isChecked = dataValue == 1
                }

                ADDRESS_DESK_HEIGHT_GET -> {

                    heightValue = dataValue / 10
                    showDeskHeight(heightValue)
                    KLog.i("高度", "${heightValue}")
                    // this.currentHeight = currentHeight
                    //  showDeskHeight(currentHeight)
                }

                ADDRESS_DESK_MODE_GET -> {
                    /* var modeStand = getIntValue(it.value, 13) / 10
                      var modeSit = getIntValue(it.value, 15) / 10*/
                    // showCenterToast("${modeSit}cm ${modeStand}cm ")
                    //   updateModeDataFromDevice(modeSit, modeStand)
                }

                ADDRESS_DESK_TIMER_GET, ADDRESS_DESK_TIMER -> {
                    KLog.i("闹钟提醒", "${dataValue}")
                    //info(TAG, "listenDeviceData: ")
                    isClockEnableText = if (dataValue != 0) getLanguage(
                        "common_open",
                        "已开启"
                    ) else getLanguage("common_closed", "未开启")
                    clockMin = dataValue
                    //binding.tvDeskClock.text = showText
                }
            }
        }

    }


    override fun pageWillDestroy() {
        super.pageWillDestroy()
        acquireModule<NotifyModule>(NotifyModule.MODULE_NAME).removeNotify(
            "BleData",
            eventCallbackRef
        )
        acquireModule<NotifyModule>(NotifyModule.MODULE_NAME).removeNotify(
            DEVICE_UNBIND,
            devcieUnbindbackRef
        )
        acquireModule<NotifyModule>(NotifyModule.MODULE_NAME).removeNotify(
            DESK_MODE_LIST,
            deskModeListCallbackRef
        )
        acquireModule<NotifyModule>(NotifyModule.MODULE_NAME).removeNotify(
            DEVICE_CONNECT_STATE,
            devcieConnectStatebackRef
        )
    }

    override fun pageDidDisappear() {
        super.pageDidDisappear()
        KLog.i("pageDidDisappear", "")
        acquireModule<SharedPreferencesModule>(SharedPreferencesModule.MODULE_NAME)
            .setInt(DEVICE_HEIGHT, heightValue)
    }

    override fun pageDidAppear() {
        super.pageDidAppear()

        titleDeviceName =
            acquireModule<SharedPreferencesModule>(SharedPreferencesModule.MODULE_NAME)
                .getString(DEVICE_NAME)

    }

    override fun layoutFrameDidChanged(frame: Frame) {
        super.layoutFrameDidChanged(frame)
        updateViewWidth(frame.width)
    }

    fun saveDeviceName(name: String) {
        acquireModule<SharedPreferencesModule>(SharedPreferencesModule.MODULE_NAME)
            .setString(DEVICE_NAME, name)
    }

    private fun updateViewWidth(containerWidth: Float) {

        viewWidth = (containerWidth - VIEW_PADDING * 2 - VIEW_SPACING) / 2
    }

    fun queryDeskModeList() {
        var pageJson = pagerData.params
        pageJson?.let {
            var localBaseUrl = it.optString("baseUrl")
            if (!localBaseUrl.isNullOrEmpty()) {
                baseUrl = localBaseUrl
            }
            var token = it.optString("token")
            var userId = it.optInt("userId")
            var eqId = it.optInt("equipmentId")
            var jsonObject = JSONObject()
            jsonObject.put("equipmentId", eqId)
            jsonObject.put("token", token)
            jsonObject.put("userId", userId)

            var jsonObjectHeader = JSONObject()
            jsonObjectHeader.put("Content-Type", "application/json")
            acquireModule<NetworkModule>(NetworkModule.MODULE_NAME).httpRequest(
                "${baseUrl}app/equipment/info/query",
                true,
                jsonObject,
                headers = jsonObjectHeader,
                responseCallback = { data, success, errorMsg, networkResponse ->
                    var content = data.optJSONObject("content")
                    var code = data.optInt("code")
                    if (networkResponse.statusCode == 200) {
                        if (code == 0) {
                            content?.let {
                                updateDeskListInfo(content)
                                val bridgeModule =
                                    PagerManager.getCurrentPager()
                                        .getModule<BridgeModule>(BridgeModule.MODULE_NAME)
                                bridgeModule?.sendNotify(DESK_MODE_LIST, data)
                                KLog.i("content", "queryDeskModeList:$it ")
                            }
                            /*  toastMsg="更新模式成功"
                              showMsgDialog=true*/
                        }
                    }


                    KLog.i("ss", "${data.toString()} $success $errorMsg $networkResponse")
                })
        }
    }

    fun updateDeskListInfo(content: JSONObject?) {
        content?.let {
            acquireModule<SharedPreferencesModule>(SharedPreferencesModule.MODULE_NAME)
                .setObject("DeskModeList", content)
            KLog.i("设备主页DeskModeList", content.toString())
            deskModeList.clear()
            deskModeList.addAll(getDeskModeList(content))
            deskModeList.forEachIndexed { index, customDeskModeJson ->
                customDeskModeJson?.let {
                    if (it.isSelected == true) {
                        seletedDeskMode = it
                        currentMode = index
                    }
                }
            }

        }

    }

    fun showDeskHeight(height: Int) {
        checkModeHeight(height)
        //117-72
        var totalHeight = 45f
        var showHeight = height - 72
        var ratio = showHeight / totalHeight
        ratio = ratio.coerceIn(0f, 1f)

        var maxHeight = 30f
        deskTop = 30 - maxHeight * ratio
        /*   var layoutParams = binding.ivDeskTop.layoutParams as RelativeLayout.LayoutParams
           layoutParams.topMargin = (deskTopMargin - maxHeight * ratio).toInt()
           binding.ivDeskTop.layoutParams = layoutParams*/
    }

    //当前高度不等于选中模式就取消掉选中框
    fun checkModeHeight(height: Int) {
        KLog.i(
            "检查高度",
            "$height ${seletedDeskMode} ${seletedDeskMode?.sit} ${seletedDeskMode?.stand}"
        )
        seletedDeskMode?.let {
            var selectedHeightArray = arrayOf(it.sit, it.stand, it.custom)
            if (modeSelected) {
                if (selectedHeightArray.indexOf(height) + 1 == diyModeSelectedIndex) {
                    modeSelected = false
                }
            } else {
                diyModeSelectedIndex = selectedHeightArray.indexOf(height) + 1
            }

        }
    }

    fun updateDeskMode(position: Int) {

        var pageJson = pagerData.params
        pageJson?.let {

            var token = it.optString("token")
            var userId = it.optInt("userId")
            var eqId = it.optInt("equipmentId")
            var jsonObject = JSONObject()
            jsonObject.put("equipmentId", eqId)
            jsonObject.put("token", token)
            jsonObject.put("userId", userId)
            var jsonArray = JSONArray()
            deskModeList.forEachIndexed { index, deskMode ->
                var jsonChild = JSONObject()
                jsonChild.put("custom", deskMode.custom)
                jsonChild.put("isAuto", deskMode.isAuto)
                jsonChild.put("isSelected", index == position)
                jsonChild.put("name", deskMode.name)
                jsonChild.put("sit", deskMode.sit)
                jsonChild.put("stand", deskMode.stand)
                jsonArray.put(jsonChild)
            }
            jsonObject.put("customDeskModeJson", jsonArray)
            // jsonObject.put("customDeskModeJson",)
            var jsonObjectHeader = JSONObject()
            jsonObjectHeader.put("Content-Type", "application/json")
            acquireModule<NetworkModule>(NetworkModule.MODULE_NAME).httpRequest(
                "${baseUrl}app/equipment/electricDesk/updateConfig",
                true,
                jsonObject,
                headers = jsonObjectHeader,
                responseCallback = { data, success, errorMsg, networkResponse ->

                    var content = data.optJSONObject("content")
                    var code = data.optInt("code")
                    if (code == 0) {

                        queryDeskModeList()
                    }
                    KLog.i("content", "updateDeskMode:$it ")

                })
        }
    }
}
